# -*- coding:utf-8 -*-

from typing import List
from datetime import datetime, time
from sqlalchemy import and_, delete, desc, func, or_, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.seedlings_do import Seedlings
from module_admin.entity.vo.seedlings_vo import SeedlingsPageModel, SeedlingsModel
from utils.page_util import PageUtil, PageResponseModel


class SeedlingsDao:

    @classmethod
    async def get_by_id(cls, db: AsyncSession, seedlings_id: int) -> Seedlings:
        """根据主键获取单条记录"""
        seedlings = (((await db.execute(
                            select(Seedlings)
                            .where(Seedlings.id == seedlings_id)))
                       .scalars())
                       .first())
        return seedlings

    """
    查询
    """
    @classmethod
    async def get_seedlings_list(cls, db: AsyncSession,
                             query_object: SeedlingsPageModel,
                             data_scope_sql: str = None,
                             is_page: bool = False) -> [list | PageResponseModel]:

        query = (
            select(Seedlings)
            .where(
                Seedlings.breeding_time == query_object.breeding_time if query_object.breeding_time else True,
                Seedlings.description == query_object.description if query_object.description else True,
                Seedlings.display_name == query_object.display_name if query_object.display_name else True,
                Seedlings.management_name == query_object.management_name if query_object.management_name else True,
                Seedlings.type == query_object.type if query_object.type else True,
                Seedlings.del_flag == '0',
                eval(data_scope_sql) if data_scope_sql else True,
            )
            .order_by(desc(Seedlings.create_time))
            .distinct()
        )
        seedlings_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
        return seedlings_list


    @classmethod
    async def add_seedlings(cls, db: AsyncSession, add_model: SeedlingsModel, auto_commit: bool = True) -> Seedlings:
        """
        增加
        """
        seedlings =  Seedlings(**add_model.model_dump(exclude_unset=True))
        db.add(seedlings)
        await db.flush()
        if auto_commit:
            await db.commit()
        return seedlings

    @classmethod
    async def edit_seedlings(cls, db: AsyncSession, edit_model: SeedlingsModel, auto_commit: bool = True) -> Seedlings:
        """
        修改
        """
        edit_dict_data = edit_model.model_dump(exclude_unset=True)
        await db.execute(update(Seedlings), [edit_dict_data])
        await db.flush()
        if auto_commit:
            await db.commit()
        return await cls.get_by_id(db, edit_model.id)

    @classmethod
    async def del_seedlings(cls, db: AsyncSession, seedlings_ids: List[str], soft_del: bool = True, auto_commit: bool = True):
        """
        删除
        """
        if soft_del:
            await db.execute(update(Seedlings).where(Seedlings.id.in_(seedlings_ids)).values(del_flag='2'))
        else:
            await db.execute(delete(Seedlings).where(Seedlings.id.in_(seedlings_ids)))
        await db.flush()
        if auto_commit:
            await db.commit()